#include "ThreadLinks.h"

#include <QDebug>

ThreadLinks::ThreadLinks()
{
    soc = new SocketTCP();
}

ThreadLinks::~ThreadLinks()
{
}

void ThreadLinks::run()
{

}

void ThreadLinks::setIpServer(QString ipServer)
{
    this->ipServer = ipServer;
}

void ThreadLinks::setPort(QString port)
{
    this->port = port;
}

void ThreadLinks::createClient()
{
    client = soc->generateClient(this->ipServer, this->port);

    connect(client, SIGNAL(readyRead()), this, SLOT(receiveData()));
}

void ThreadLinks::createServer()
{
    connect(soc, SIGNAL(newClient(QTcpSocket*)), this, SLOT(addNewClient(QTcpSocket*)));
    connect(soc, SIGNAL(ipServer(QString)),this, SLOT(saveIP(QString)));
    server = soc->generateServer(this->port);
}

void ThreadLinks::sendData(char* data, quint32 sizeData)
{
    if(listClients.size() == 0)
        client->write(data, sizeData);
    else
        for (int i = 0; i < listClients.size(); i++)
            listClients.at(i)->write(data, sizeData);
}

void  sendDataTo(char* data, quint32 sizeData, QTcpSocket *socket)
{
    socket->write(data, sizeData);
}

void ThreadLinks::receiveData()
{
    QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
    bool firstPacket = true;
    int  sizeSocketData = 0;
    int  sizeReadData = 0;
    if (socket == 0)
        return;
    while (socket->bytesAvailable())
    {
        if(firstPacket)
        {
            socket->read((char *)&header, sizeof(headerTCP));
            buffer = new char[header.bufferSize];
            sizeSocketData = header.bufferSize;
        }

        sizeReadData = socket->read(&(buffer[header.bufferSize-sizeSocketData]), sizeSocketData);

        if(sizeReadData < sizeSocketData) // IL Y A PLEIN DE MORCEAUX
        {
            sizeSocketData-=sizeReadData;
            firstPacket = false;
        }
        else
        {
            qDebug() << "idCommmande : " << header.idCommande << endl;
            qDebug() << "idAction : " << header.idAction << endl;
            qDebug() << "bufferSize : " << header.bufferSize << endl;
        }
    }
}

void ThreadLinks::addNewClient(QTcpSocket* client)
{
    listClients.append(client);
    connect(client, SIGNAL(readyRead()), this, SLOT(receiveData()));
    headerTCP head1;
    head1.idCommande = '1';
    head1.idAction= '1';
    head1.bufferSize = 0;
    sendData((char*)&head1, sizeof(headerTCP));

    headerTCP head2;
    head2.idCommande = '2';
    head2.idAction= '2';
    head2.bufferSize = 0;
    sendData((char*)&head2, sizeof(headerTCP));

    QImage img("C:/Users/Nimis/Desktop/jdr-en-ligne/sources/Img/character_male.png");
    headerTCP head3;
    head3.idCommande = '3';
    head3.idAction= '3';
    head3.bufferSize = sizeof(headerTCP)+img.byteCount();

    char  buffer[img.byteCount()+sizeof(headerTCP)];
    strcpy(buffer, (char*)&head3);
    strcat(buffer, (char *)img.bits());
    QMessageBox::information(0, "1", QString::number(img.byteCount()));
    sendData(buffer,sizeof(headerTCP)+img.byteCount());


}

QString ThreadLinks::getIp()
{
    return ipServer;
}

void ThreadLinks::saveIP(QString ip)
{
    this->ipServer = ip;
    qDebug() << "L'ip du serveur = " << this->ipServer;
}


